Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
Which is the equivalent in Mongoose of this MongoDb query db.collection.find( { tags: { $all: [some elements in the array] } } )

I am trying to find an equivalent to this MongoDB query db.inventory.find( { tags: { $all: ["red", "blank"]}}) in Mongoose. I found this one in Mongoose Model.find({'field': { $in: array name }) but it gives me as results documents with all elements in the array in a document and also documents that matches at least one of the elements in the array.

Here is a code I am using to display artisans with specific specialties:

display artisans profiles route

app.post('/artisanprofile', function(req, res){
  const profile = [];
  const result = req.body.search;
  console.log(result);
  console.log(typeof result);
  const specialities = result.split(" ");
  console.log(specialities);
  Artisan.find({
    'speciality': { $in: specialities }
}, function(err, profiles) {
  if(err){
    console.log(err);
  }
  else{
    console.log(profiles);
  }
});
});

the documents I am looking for are ones with specialties test and test1 but I get all documents with at least one of the elements in the array specialties passed to the query.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

try it use and and for each element of specialities use $in

app.post('/artisanprofile', function(req, res){
  const profile = [];
  const result = req.body.search;
  console.log(result);
  console.log(typeof result);
  const specialities = result.split(" ");
  console.log(specialities);
  var andObj = {}
  var andArr = []
  specialities.forEach(item=>{
      var itemArr = []
      itemArr.push(item)
      andObj['$in'] :itemArr
      andArr.push({speciality:andObj})
      andObj = {}
  })
  Artisan.find({
    $and : andArr
}, function(err, profiles) {
  if(err){
    console.log(err);
  }
  else{
    console.log(profiles);
  }
});
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda